package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/11/28 16:46
 */

// 暴露服务端口
@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 保存基本信息
     *
     * @param userInfo
     */
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    /**
     * 补充头像
     *
     * @param userInfo
     */
    @Override
    public void updateHead(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 查询用户详细信息
     *
     * @param id
     * @return
     */
    @Override
    public UserInfo findById(Long id) {
        UserInfo userInfo = userInfoMapper.selectById(id);
        return userInfo;
    }

    /**
     * 用户信息更新
     *
     * @param userInfo
     */
    @Override
    public void updateInfo(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * UserInfo分页查询
     *
     * @param userInfo
     * @param ids
     * @return
     */
    @Override
    public Map<Long, UserInfo> findByIds(UserInfo userInfo, List<Long> ids) {


        LambdaQueryWrapper<UserInfo> queueWrapper = new LambdaQueryWrapper<>();
        // 构建查询条，在UserInfo中查询存在id的
        queueWrapper.in(UserInfo::getId, ids);

        // 动态条件
     /*   if(userInfo != null){
            queueWrapper.lt(userInfo.getAge() != null, UserInfo::getAge, userInfo.getAge());
            queueWrapper.eq(!StringUtils.isEmpty(userInfo.getGender()), UserInfo::getGender, userInfo.getGender());
            queueWrapper.like(!StringUtils.isEmpty(userInfo.getNickname()), UserInfo::getNickname, userInfo.getNickname());
        }*/

        //2、添加筛选条件
        if (userInfo != null) {
            if (userInfo.getAge() != null) {
                queueWrapper.lt(UserInfo::getAge, userInfo.getAge());
            }
            if (!StringUtils.isEmpty(userInfo.getGender())) {
                queueWrapper.eq(UserInfo::getGender, userInfo.getGender());
            }
            if (!StringUtils.isEmpty(userInfo.getNickname())) {
                queueWrapper.like(UserInfo::getNickname, userInfo.getNickname());
            }
        }
        List<UserInfo> userInfos = userInfoMapper.selectList(queueWrapper);
        // hutool工具将list转换为map
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(userInfos, "id");
        return map;
    }

    /**
     * 查询后台所有用户列表
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public IPage<UserInfo> findAll(Integer page, Integer pagesize) {

        //
        Page<UserInfo> page1 = new Page<>(page, pagesize);
        LambdaQueryWrapper<UserInfo> query = new LambdaQueryWrapper<>();
        query.eq(UserInfo::getUserStatus, "1");
        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(page1, query);
        return userInfoPage;
    }
}
